/**
* 修改仓库
*/
<template>
	<vp-dialog ref="editDialogRef" :span="1">
		<template #title>
			<vp-label-add v-if="type === 'add'" />
			<vp-label-edit v-else />
		</template>
		<el-form v-loading="loading" :model="formData" :rules="formRules" ref="formRef" label-width="120px"
			:scroll-to-error="true">
			<el-row>
				<vp-col :span="1">
					<el-form-item prop="lb">
						<template #label><vp-label zh="类别" en="Type" align="left" /></template>
						<el-select v-model="formData.lb" placeholder="请选择">
							<el-option label="可用库" :value="10"><vp-label zh="可用库"
									en="Available Warehouse" /></el-option>
							<el-option label="待修库" :value="20"><vp-label zh="待修库"
									en="To Be Repaired Warehouse" /></el-option>
							<el-option label="报废库" :value="30"><vp-label zh="报废库" en="Scrap Warehouse" /></el-option>
							<el-option label="隔离库" :value="40"><vp-label zh="隔离库" en="Isolated Warehouse" /></el-option>
						</el-select>
					</el-form-item>
				</vp-col>
				<vp-col :span="1">
					<el-form-item prop="jdid">
						<template #label><vp-label zh="基地" en="Base" /></template>
						<VpBaseSelectTree :selectedId="formData.jdid" @treeSelected="handleBaseSelected" class="w100">
						</VpBaseSelectTree>
					</el-form-item>
				</vp-col>
				<vp-col :span="1">
					<el-form-item prop="bh">
						<template #label><vp-label zh="仓库号" en="Warehouse No." align="left" /></template>
						<el-input v-model="formData.bh" v-trim maxlength="100">
						</el-input>
					</el-form-item>
				</vp-col>
				<vp-col :span="1">
					<el-form-item prop="mc">
						<template #label><vp-label zh="仓库名称" en="Warehouse Name" align="left" /></template>
						<el-input v-model="formData.mc" v-trim maxlength="100"></el-input>
					</el-form-item>
				</vp-col>
				<vp-col :span="1">
					<el-form-item prop="bz">
						<template #label><vp-label zh="备注" en="Remark" align="left" /></template>
						<el-input type="textarea" v-model="formData.bz" v-trim maxlength="1000"></el-input>
					</el-form-item>
				</vp-col>
			</el-row>
		</el-form>
		<template #footer>
			<vp-btn-confirm :loading="saveLoading" @click="handleSave" />
		</template>
	</vp-dialog>
</template>
<script setup lang="ts" name="editWarehouse">
import { ref } from 'vue'
import request from '/@/utils/request';
import validateUtils from '/@/utils/validateUtils';
import VpBaseSelectTree from '/@/components/vp/base/VpBaseSelectTree.vue';

const emits = defineEmits(['formSubmited']);

const formData = ref({
	id: "",
	bh: "",
	mc: "",
	bz: "",
	lb: null,
	jdid: ""
});

const formRules = {
	bh: [validateUtils.notNull, validateUtils.notChinese],
	mc: [validateUtils.notNull]
}

const loading = ref(false);
const saveLoading = ref(false);
const type = ref('add');
const editDialogRef = ref();
const formRef = ref();

// 打开弹窗
const handleOpen = (__type: string, id: string) => {
	type.value = __type;
	initFormData();
	editDialogRef.value.handleOpen();
	if (__type === 'edit') {
		loading.value = true;
		request.get(`/masterData/warehouse/${id}`).then((res) => {
			formData.value = {
				...formData.value,
				...res.data
			};
			loading.value = false;
		})
	}
};

// 清空数据
const initFormData = () => {
	formData.value = {
		id: "",
		bh: "",
		mc: "",
		bz: "",
		lb: null,
		jdid: ""
	}
}

// 保存模块
const handleSave = () => {
	if (loading.value) {
		return;
	}
	saveLoading.value = true;
	formRef.value.validate((valid: Boolean) => {
		if (valid) {
			request.post("/masterData/warehouse/save", {
				...formData.value
			}).then(() => {
				emits("formSubmited");
				editDialogRef.value.handleClose();
			}).catch(() => {
			}).finally(() => {
				saveLoading.value = false;
			})
		} else {
			saveLoading.value = false;
		}
	})
}

// 选择基地后回调事件
const handleBaseSelected = (data: any) => {
	formData.value.jdid = data.id;
}


defineExpose({
	handleOpen,
})
</script>
